package arquitetura.view;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import exceptions.GeralException;

import arquitetura.fachada.Fachada;

import to.Genero;
import util.DocumentoCaixaAlta;

public class ViewGeneroRec extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3668844666707028922L;
	private JTextField txtDescricao;
	private Fachada fachada;
	private Genero genero;
	private static int codigo;
	public static boolean generoSalvo = false;

	/**
	 * Launch the application.
	 */
	public static void exibeTela(int codigoGenero) {
		codigo = codigoGenero;
		//EventQueue.invokeLater(new Runnable() {
			//public void run() {
				try {
					ViewGeneroRec dialog = new ViewGeneroRec();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setLocationRelativeTo(null);
					dialog.setModal(true);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			//}
		//});
	}

	/**
	 * Create the dialog.
	 */
	public ViewGeneroRec() {
		fachada = Fachada.getInstancia();
		genero = new Genero();
		
		setTitle("Cadastro de G\u00EAneros");
		setBounds(100, 100, 383, 195);
		getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(10, 114, 347, 36);
		getContentPane().add(panel);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					preencherDadosGenero();
					
					if (codigo == 0) {						
						generoSalvo = fachada.incluirGenero(genero);
							if (generoSalvo)
								limparDadosDaTela();					
					} else {
						generoSalvo = fachada.alterarGenero(genero);
						if (generoSalvo)
							dispose();
					}					
				} catch (GeralException e1) {
					JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnSalvar.setBounds(149, 5, 89, 23);
		panel.add(btnSalvar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancelar.setBounds(248, 5, 89, 23);
		panel.add(btnCancelar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(240, 248, 255));
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_1.setBounds(10, 11, 347, 97);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(176, 224, 230)));
		panel_2.setBackground(new Color(255, 255, 255));
		panel_2.setBounds(10, 11, 327, 73);
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o");
		lblDescrio.setBounds(10, 11, 60, 14);
		panel_2.add(lblDescrio);
		
		txtDescricao = new JTextField();
		txtDescricao.setColumns(10);
		txtDescricao.setBounds(10, 29, 307, 20);
		txtDescricao.setDocument(new DocumentoCaixaAlta(40));
		panel_2.add(txtDescricao);
		
		if (codigo > 0){
			try {
				genero = fachada.consultarGenero(codigo);
				preencherControlesComDadosGenero();
			} catch (GeralException e1) {
				JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
			}			
		}

	}
	
	private void preencherDadosGenero(){
		genero.setDescricao(txtDescricao.getText());
	}
	
	private void limparDadosDaTela(){
		txtDescricao.setText(null);
	}
	
	private void preencherControlesComDadosGenero(){
		txtDescricao.setText(genero.getDescricao());
	}
}
